package com.seu.algorithms.utils;

import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 用*号混淆中间的字符串
 *
 * @author liangfeihu
 * @since 2018/12/19 10:33
 */
public class MiscUtil {
    public static Pattern REAL_NAME_PATTERN = Pattern.compile("^([a-zA-Z0-9\\u4e00-\\u9fa5\\·]{1,15})$");

    //遮蔽手机号中间字符串
    public static String obscuredPhone(String phone) {
        char[] array = phone.toCharArray();
        for (int i = 0; i < array.length - 4; ++i) {
            if (i > 2) {
                array[i] = 42;
            }
        }
        return new String(array);
//        return phone.substring(0, 3) + "****" + phone.substring(7);
    }


    //遮蔽银行卡中间字符串
    public static String obscuredBankCardNo(String cardNo) {
        char[] array = cardNo.toCharArray();
        for (int i = 0; i < array.length - 4; ++i) {
            array[i] = 42;
        }
        return new String(array);
    }

    //遮蔽银行卡中间字符串
    public static String obscuredIdCardNo(String cardNo) {
        char[] array = cardNo.toCharArray();
        for (int i = 0; i < array.length - 4; ++i) {
            if (i > 2) {
                array[i] = 42;
            }
        }
        return new String(array);
    }

    //判断是否是数字
    public static boolean isNumeric(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }


    public static Boolean isMobilePhone(String phone) {
        if (StringUtils.isEmpty(phone)
                || phone.trim().length() != 11) {
            return false;
        }
        String result = phone.trim();
        if (result.startsWith("+86")) {
            result = result.substring(3);
        }

        if (result.startsWith("86")) {
            result = result.substring(2);
        }

        return Boolean.valueOf(isMatch("^1[3456789]\\d{9}$", result));
    }

    public static boolean isMatch(String regex, String phoneNo) {
        Pattern pattern = Pattern.compile(regex);
        Matcher match = pattern.matcher(phoneNo);
        return match.matches();
    }

    private static boolean isNotEmojiCharacter(char codePoint) {
        return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD)
                || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
                || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
    }

    public static String filterEmoji(String source) {
        int len = source.length();
        StringBuilder buf = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (isNotEmojiCharacter(codePoint)) {
                buf.append(codePoint);
            } else {
                buf.append("*");
            }
        }
        return buf.toString();
    }

    public static String getExclusionProducts(String exclusionProducts) {
        String[] exclusionProductArray = exclusionProducts == null ? new String[]{} : exclusionProducts.split(",");
        String excluProsTemp = "'" + org.apache.commons.lang3.StringUtils.join(exclusionProductArray, "','") + "'";
        return excluProsTemp;
    }


    public static Boolean isRealName(String name) {
        return REAL_NAME_PATTERN.matcher(name).matches();
    }


}
